﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using LibNiconico.NiconicoApi;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Videos;
using LibNiconico.NiconicoApi.Lives;
using LibNiconico.NiconicoApi.Utils;
using LibNiconico.NiconicoApi.Internal.Http;
using LibNiconico.NiconicoApi.User;
using LibNiconico.NiconicoApi.Mylist;
using LibNiconico.NiconicoApi.Login;
using System.Threading;
using LibNiconico.NiconicoApi.Lives.Broadcast;

namespace LibNiconico.NiconicoApi
{
    /// <summary>
    /// ニコニコのAPIヘルパークラス
    /// </summary>
    public sealed class NicoApiHelper
    {



        /// <summary>
        /// コンストラクタ
        /// </summary>
        public NicoApiHelper()
        {

            this.Auth = new NiconicoAuth();

            this.DefaultCreateInfo = new NiconicoApiInfoGetCreateInfo();

            this.VideoApi = new NicoVideoApiHelper(this.Auth, this.DefaultCreateInfo);

            this.LiveApi = new NicoLiveApiHelper(this.Auth, this.DefaultCreateInfo);

            this.UserApi = new NicoUserApiHelper(this.Auth, this.DefaultCreateInfo);

            this.MylistApi = new NicoMylistApiHelper(this.Auth, this.DefaultCreateInfo);

            this.BroadcastApi = new NicoLiveBroadcastApiHelper(this.Auth, this.DefaultCreateInfo);
            
        }

        

        /// <summary>
        /// 非同期にニコニコへログインします
        /// </summary>
        /// <param name="mail">ログインメールアドレス</param>
        /// <param name="password">ログインパスワード</param>
        /// <returns>ログインの成否が格納されます</returns>
        public async Task<NicoLoginResponse> LoginAsync(string mail, string password)
        {

            return await LoginAsync(new NicoLoginQueryInfo(mail, password)).ConfigureAwait(false);
        }

        internal async Task<NicoLoginResponse> LoginAsync(NicoLoginQueryInfo queryInfo)
        {
            using(var api = new NiconicoInfoGetApiCookieImpl(NicoSecureConstant.LOGIN_URL,NiconicoHttpClient.CreateNiconicoApi()))
            {
                var response = NicoLoginResponse.Parse((await api.PostCookieAsync(queryInfo).ConfigureAwait(false)).Content);

                if (response.Succeed)
                {
                    this.Auth.SetUserSession(response.Auth.UserSession);
                }

                return response;
            }
        }

        /// <summary>
        /// ユーザセッションを取得
        /// </summary>
        /// <returns></returns>
        public Cookie GetUserSession()
        {
            return this.Auth.GetUserSession();
        }

        /// <summary>
        /// ユーザセッションを設定
        /// </summary>
        /// <param name="userSession"></param>
        public void SetUserSession(Cookie userSession)
        {
            this.Auth.SetUserSession(userSession);
        }


        /// <summary>
        /// 認証情報が設定されているか判定
        /// </summary>
        public bool IsSetAuth
        {
            get
            {
                return this.Auth.IsSetAuth;
            }
        }



        /// <summary>
        /// 認証情報をクリア
        /// </summary>
        public void ClearAuth()
        {
            this.Auth.Clear();
        }


        /// <summary>
        /// ヘルパーに関連付いた認証情報
        /// </summary>
        internal NiconicoAuth Auth { get; private set; }

        /// <summary>
        /// 既定のAPI生成情報
        /// </summary>
        internal NiconicoApiInfoGetCreateInfo DefaultCreateInfo
        {
            get;
            private set;
        }

        /// <summary>
        /// ニコニコ動画APIヘルパー
        /// </summary>
        public NicoVideoApiHelper VideoApi
        {
            get;
            private set;
        }

        /// <summary>
        /// ニコニコ生放送APIヘルパー
        /// </summary>
        public NicoLiveApiHelper LiveApi
        {
            get;
            private set;
        }

        /// <summary>
        /// ニコニコユーザAPIヘルパー
        /// </summary>
        public NicoUserApiHelper UserApi
        {
            get;
            private set;
        }

        /// <summary>
        /// ニコニコマイリストAPIヘルパー
        /// </summary>
        public NicoMylistApiHelper MylistApi
        {
            get;
            private set;
        }

        /// <summary>
        /// ニコ生配信APIヘルパー
        /// </summary>
        public NicoLiveBroadcastApiHelper BroadcastApi
        {
            get;
            private set;
        }
    }
}
